package org.cresource.quartz.service.impl;

import org.cresource.basic.util.QuartzUtils;
import org.cresource.quartz.domain.QuartzJobInfo;
import org.cresource.quartz.job.MainJob;
import org.cresource.quartz.service.IQuartzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class QuartzServiceImpl implements IQuartzService {
    @Autowired
    private SchedulerFactoryBean schedulerFactory;

    /*
     *
     * @param scheduler
     * @param jobName
     * @param cls
     * @param params
     * @param time 时间表达式 定时任务执行的时间   已知 执行的时间  Date--->0/1 * * * * ?
     */
    @Override
    public void addQuartz(QuartzJobInfo quartzJobInfo) {
        /*
           Scheduler sched, String jobName, @SuppressWarnings("rawtypes") Class cls, Object params,
             String time
         */
        try {
            QuartzUtils.addJob(schedulerFactory.getScheduler(),quartzJobInfo.getJobName() , MainJob.class,quartzJobInfo.getParams() , quartzJobInfo.getCronj());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void removeQuartz(String orderSn) {
        QuartzUtils.removeJob(schedulerFactory.getScheduler(),orderSn);
    }

}
